@section styles{
    <link href="/adminres/layui/lay/extend/treetable.css" rel="stylesheet" />
}
<script type="text/html" id="toolbarList">
    <div class="layui-btn-container">
        @Html.Raw(ViewBag.ButtonHtml)
        <button class="layui-btn layui-btn-sm" lay-event="expand" id="btn-expand">展开 / 折叠</button>
        <button class="layui-btn layui-btn-sm" lay-event="refresh" id="btn-refresh">刷新列表</button>
    </div>
</script>
<script type="text/html" id="rowToolbar">
    @Html.Raw(ViewBag.ButtonHtml2)
</script>
<table class="layui-hide" lay-filter="tblList" id="tblList"></table>
@section Scripts{
    <script type="text/javascript">
        var renderTable;
        /*使用模块加载的方式 加载文件*/
        layui.extend({
            treetable: '/adminres/layui/lay/extend/treetable'
        }).use(['layer', 'table', 'treetable', 'form', 'jquery'], function () {
            var $ = layui.jquery;
            var table = layui.table;
            var layer = layui.layer;
            var treetable = layui.treetable;
            var form = layui.form;

            // 渲染表格
            renderTable = function (where) {//参考文档：https://gitee.com/whvse/treetable-lay
                layer.load(2);
                treetable.render({
                    toolbar: '#toolbarList',
                    treeColIndex: 1,//树形图标显示在第几列
                    treeSpid: '-1',//最上级的父级id
                    treeIdName: 'id',//id字段的名称
                    treePidName: 'parentId',//pid字段的名称
                    treeDefaultClose: false,//是否默认折叠
                    treeLinkage: true,//父级展开时是否自动展开所有子级
                    elem: '#tblList',
                    url: '/admin/articleCategory/getlist', //数据接口
                    where: where,
                    page: false,
                    cols: [[
                        { type: 'numbers', title: '#' }
                        , { field: 'name', title: '分类名称' }
                        , { field: 'sort', width: 60, title: '排序' }
                        , { templet: '#rowToolbar', width: 200, align: 'center', title: '操作' }
                    ]]
                    , done: function () {
                        layer.closeAll('loading');
                    }
                });
            };

            renderTable();

            //折叠/展开
            var expand = true;

            //按条件搜索数据
            form.on('submit(formSearch)', function (data) {
                renderTable(data.field);
                return false;
            });
            //监听事件
            table.on('toolbar(tblList)', function (obj) {
                //var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'add': win.open("/admin/articleCategory/addedit", 550, 450, "新增"); break;
                    case 'expand':
                        if (expand) {
                            treetable.foldAll('#tblList');
                            expand = false;
                        } else {
                            treetable.expandAll('#tblList');
                            expand = true;
                        }
                        break;
                    case 'refresh': renderTable({ 'KeyWords': $('input[name=KeyWords]').val() }); break;
                };
            });
            //监听工具条
            table.on('tool(tblList)', function (obj) {
                var data = obj ? obj.data : null;
                if (data == null || $(this).hasClass("layui-btn-disabled")) {
                    return;
                }

                switch (obj.event) {
                    case 'del': del(data); break;
                    case 'add': win.open("/admin/articleCategory/addedit", 550, 450, "新增"); break;
                    case 'edit': win.open("/admin/articleCategory/addedit?id=" + data.id + "&pid=" + data.parentId + "&level=" + data.level, 550, 450, "修改"); break;
                };
            });


            //单个删除
            var del = function (data) {
                layer.confirm("你确定删除[" + data.name + "]分类吗？如果存在下级节点则一并删除，此操作不能撤销！", { icon: 3, title: '提示' },
                    function (index) {
                        $.ajax({
                            type: "post",
                            dataType: 'json',
                            url: '/admin/articleCategory/remove',
                            data: { ids: data.id },
                            success: function (res) {
                                if (res.code == 0) {
                                    win.alert('删除成功');
                                    renderTable();//重新加载表格数据
                                }
                                else {
                                    win.alert(res.message);
                                }
                            },
                            error: function (res) {
                                win.alert("请求出错");
                                console.log(res);
                            }
                        });
                    }
                );
            };
        });
        function getList() {
            renderTable();
        }
    </script>
}